﻿Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.CompilerServices
Imports System.IO
Imports System.Text
Imports System.Net
Imports System.Web
Imports System.Collections.Specialized
Imports Microsoft.Win32
Imports Zytonic_Screenshot.ItemLibrary

Public Class FrmSettings
#Region "Functions"
    Private Declare Sub ReleaseCapture Lib "user32" ()
    Private Const WM_NCLBUTTONDOWN = &HA1
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal process As IntPtr, ByVal minimumWorkingSetSize As Integer, ByVal maximumWorkingSetSize As Integer) As Integer
    Public Shared Function CreateShortcut(ByVal Target_Path As String, ByVal Shortcut_Path As String, ByVal Shortcut_Name As String, _
   ByVal Working_Directory As String, ByVal Arguments As String, ByVal Window_Style As Integer, ByVal Icon_Num As Integer) As Object
        Dim objectValue As Object = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""))
        Dim objectValue2 As Object = RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(objectValue, Nothing, _
        "CreateShortcut", New Object() {Shortcut_Path & "\" & Shortcut_Name & ".lnk"}, Nothing, Nothing, Nothing))
        NewLateBinding.LateSet(objectValue2, Nothing, "TargetPath", New Object() {Target_Path}, Nothing, Nothing)
        NewLateBinding.LateSet(objectValue2, Nothing, "WorkingDirectory", New Object() {Working_Directory}, Nothing, Nothing)
        NewLateBinding.LateSet(objectValue2, Nothing, "Arguments", New Object() {Arguments}, Nothing, Nothing)
        NewLateBinding.LateSet(objectValue2, Nothing, "WindowStyle", New Object() {Window_Style}, Nothing, Nothing)
        NewLateBinding.LateSet(objectValue2, Nothing, "IconLocation", New Object() {Target_Path & "," _
        & Conversions.ToString(Icon_Num)}, Nothing, Nothing)
        NewLateBinding.LateCall(objectValue2, Nothing, "Save", New Object() {}, Nothing, Nothing, Nothing, True)
        Return True
    End Function
#End Region
#Region "Theme"
    Dim LeftDown As Boolean = False
    Private Sub TopBar_MouseDown(sender As Object, e As MouseEventArgs) Handles TopBar.MouseDown
        LeftDown = True
    End Sub
    Private Sub TopBar_MouseUp(sender As Object, e As MouseEventArgs) Handles TopBar.MouseUp
        LeftDown = False
    End Sub
    Private Sub TopBar_MouseLeave(sender As Object, e As EventArgs) Handles TopBar.MouseLeave
        LeftDown = False
    End Sub
    Private Sub TopBar_MouseMove(sender As Object, e As MouseEventArgs) Handles TopBar.MouseMove
        If LeftDown = True Then
            Dim lHwnd As Int32
            lHwnd = Me.Handle
            SendMessage(lHwnd, WM_NCLBUTTONDOWN, 2, 0&)
            ReleaseCapture()
        End If
    End Sub
    Private Sub Close_MouseDown(sender As Object, e As MouseEventArgs) Handles CloseCMD.MouseDown
        CloseCMD.BackgroundImage = My.Resources.X_Down
    End Sub
    Private Sub Close_MouseUp(sender As Object, e As MouseEventArgs) Handles CloseCMD.MouseUp
        CloseCMD.BackgroundImage = My.Resources.X_Ip
    End Sub
    Private Sub Minimize_MouseDown(sender As Object, e As MouseEventArgs) Handles Minimize.MouseDown
        Minimize.BackgroundImage = My.Resources.Min_Down
    End Sub
    Private Sub Minimize_MouseUp(sender As Object, e As MouseEventArgs) Handles Minimize.MouseUp
        Minimize.BackgroundImage = My.Resources.Min_Up
    End Sub
    Private Sub Close_Click(sender As Object, e As EventArgs) Handles CloseCMD.Click
        Me.Opacity = 0.0
        Me.ShowInTaskbar = False
    End Sub
    Private Sub Minimize_Click(sender As Object, e As EventArgs) Handles Minimize.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub
    Private Sub MaximizeButton_MouseClick(sender As Object, e As MouseEventArgs)
        If Me.WindowState = FormWindowState.Maximized Then
            Me.WindowState = FormWindowState.Normal
            MaximizeButton.BackgroundImage = My.Resources.Max_Up
        Else
            Me.WindowState = FormWindowState.Maximized
            MaximizeButton.BackgroundImage = My.Resources.Max2_Up
        End If
    End Sub
    Private Sub MaximizeButton_MouseDown(sender As Object, e As MouseEventArgs)
        If Me.WindowState = FormWindowState.Maximized Then
            MaximizeButton.BackgroundImage = My.Resources.Max_Down
        Else
            MaximizeButton.BackgroundImage = My.Resources.Max2_Down
        End If
    End Sub
    Private Sub MaximizeButton_MouseHover(sender As Object, e As EventArgs)
        If Me.WindowState = FormWindowState.Maximized Then
            MaximizeButton.BackgroundImage = My.Resources.Max_Over
        Else
            MaximizeButton.BackgroundImage = My.Resources.Max2_Over
        End If

    End Sub
    Private Sub MaximizeButton_MouseLeave(sender As Object, e As EventArgs)

        If Me.WindowState = FormWindowState.Maximized Then
            MaximizeButton.BackgroundImage = My.Resources.Max_Up
        Else
            MaximizeButton.BackgroundImage = My.Resources.Max2_Up
        End If
    End Sub
    Private Sub Minimize_MouseHover(sender As Object, e As EventArgs)
        Minimize.BackgroundImage = My.Resources.Min_Over
    End Sub
    Private Sub Close_MouseHover(sender As Object, e As EventArgs) Handles CloseCMD.MouseHover
        CloseCMD.BackgroundImage = My.Resources.X_Over
    End Sub
    Private Sub Minimize_MouseLeave(sender As Object, e As EventArgs) Handles Minimize.MouseHover, Minimize.MouseLeave
        Minimize.BackgroundImage = My.Resources.Min_Up
    End Sub
    Private Sub Close_MouseLeave(sender As Object, e As EventArgs) Handles CloseCMD.MouseLeave
        CloseCMD.BackgroundImage = My.Resources.X_Ip
    End Sub
#End Region

    Private Sub FrmSettings_Load(sender As Object, e As EventArgs) Handles Me.Load
        KeyMap1.RegisterMethod("Take_Screenshot", FrmUploads)
        KeyMap1.RegisterMethod("Cancel_Screenshot", FrmUploads)
        InitializeSettings()
    End Sub
    Private Sub InitializeSettings()
        If FrmUploads.Settings.SearchItems("Auto Upload")(0).Parameters(0) = "True" Then
            CmdAuto.Toggled = True
            CmdAuto.Text = "Enabled"
            FrmUploads.UploadControl1.AutoUpload = True
        Else
            CmdAuto.Toggled = False
            CmdAuto.Text = "Disabled"
            FrmUploads.UploadControl1.AutoUpload = False
        End If

        If FrmUploads.Settings.SearchItems("Show Animation")(0).Parameters(0) = "True" Then
            CmdShowAnimation.Toggled = True
            CmdShowAnimation.Text = "Enabled"
        Else
            CmdShowAnimation.Toggled = False
            CmdShowAnimation.Text = "Disabled"
        End If

        If FrmUploads.Settings.SearchItems("Upload Location")(0).Parameters(0) = "Imgur" Then
            CmbSource.SelectedIndex = 0
        ElseIf FrmUploads.Settings.SearchItems("Upload Location")(0).Parameters(0) = "Zytonic" Then
            CmbSource.SelectedIndex = 1
        ElseIf FrmUploads.Settings.SearchItems("Upload Location")(0).Parameters(0) = "Custom..." Then
            CmbSource.SelectedIndex = 2
        End If

    End Sub

    Private Sub CmbSource_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbSource.SelectedIndexChanged
        If FrmUploads.Settings.SearchItems("Upload Location").Count <> 0 Then FrmUploads.Settings.DeleteItem("Upload Location")
        FrmUploads.Settings.AddItem(New DB_Item("Upload Location", New List(Of String) From {CmbSource.Text}))
    End Sub

    Private Sub CmdAuto_Click(sender As Object, e As EventArgs) Handles CmdAuto.Click
        If FrmUploads.Settings.SearchItems("Auto Upload")(0).Parameters(0) = "True" Then
            If FrmUploads.Settings.SearchItems("Auto Upload").Count <> 0 Then FrmUploads.Settings.DeleteItem("Auto Upload")
            FrmUploads.Settings.AddItem(New DB_Item("Auto Upload", New List(Of String) From {"False"}))
            CmdAuto.Text = "Disabled"
            FrmUploads.UploadControl1.AutoUpload = False
        Else
            If FrmUploads.Settings.SearchItems("Auto Upload").Count <> 0 Then FrmUploads.Settings.DeleteItem("Auto Upload")
            FrmUploads.Settings.AddItem(New DB_Item("Auto Upload", New List(Of String) From {"True"}))
            CmdAuto.Text = "Enabled"
            FrmUploads.UploadControl1.AutoUpload = True
        End If
    End Sub

    Private Sub CmdShowAnimation_Click(sender As Object, e As EventArgs) Handles CmdShowAnimation.Click
        If FrmUploads.Settings.SearchItems("Show Animation")(0).Parameters(0) = "True" Then
            If FrmUploads.Settings.SearchItems("Show Animation").Count <> 0 Then FrmUploads.Settings.DeleteItem("Show Animation")
            FrmUploads.Settings.AddItem(New DB_Item("Show Animation", New List(Of String) From {"False"}))
            CmdShowAnimation.Text = "Disabled"
        Else
            If FrmUploads.Settings.SearchItems("Show Animation").Count <> 0 Then FrmUploads.Settings.DeleteItem("Show Animation")
            FrmUploads.Settings.AddItem(New DB_Item("Show Animation", New List(Of String) From {"True"}))
            CmdShowAnimation.Text = "Enabled"
        End If
    End Sub
End Class